#include "userwidget.h"
#include "configargs.h"
#include <QFrame>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QFile>
#include <QVBoxLayout>
#include <QTextStream>
#include "InputMethod/inputmethod.h"

UserWidget::UserWidget(QWidget *parent) : QWidget(parent)
{
    im = new InputMethod;
    QFrame *pBlueFrame = new QFrame(this);
    pBlueFrame->setGeometry(USERWIDGET_MARGIN, USERWIDGET_MARGIN, USERWIDGET_BLUEFRAME_W, USERWIDGET_BLUEFRAME_H);

    QFrame *pWhiteFrame = new QFrame(this);
    pWhiteFrame->setGeometry(USERWIDGET_BLUEFRAME_W + USERWIDGET_MARGIN, USERWIDGET_MARGIN, USERWIDGET_WHITEFRAME_W, USERWIDGET_WHITEFRAME_H);

    QLabel *pUserNameLab = new QLabel();
    m_pUserNameEdit = new QLineEdit();
    QLabel *pUserPasswdLab = new QLabel();
    m_pUserPasswdEdit = new QLineEdit();
    QPushButton *pChangePasswdBtn = new QPushButton();

    m_pUserPasswdEdit->installEventFilter(im);
    m_pUserPasswdEdit->setObjectName("m_pUserPasswdEdit");
    connect(im,SIGNAL(setImObjName(QString)),this,SLOT(createImConnect(QString)));//connect the input method

    QVBoxLayout *pWhiteLayout = new QVBoxLayout();
    pWhiteLayout->addStretch(3);
    pWhiteLayout->addWidget(pUserNameLab);
    pWhiteLayout->addWidget(m_pUserNameEdit);
    pWhiteLayout->addWidget(pUserPasswdLab);
    pWhiteLayout->addWidget(m_pUserPasswdEdit);
    pWhiteLayout->addStretch(1);
    pWhiteLayout->addWidget(pChangePasswdBtn);

	if(g_lcdsize == LCDSIZE_480x272)
	{
		pWhiteLayout->setContentsMargins(10, 20, 10, 10);
		pWhiteLayout->setSpacing(10);
	}else{
    	pWhiteLayout->setContentsMargins(20, 40, 20, 20);
    	pWhiteLayout->setSpacing(20);
	}
    pWhiteFrame->setLayout(pWhiteLayout);

    pUserNameLab->setText(tr("当前用户："));
    pUserPasswdLab->setText(tr("用户密码："));
    pChangePasswdBtn->setText(tr("修改密码"));

    m_pUserNameEdit->setText(tr("root"));
    m_pUserPasswdEdit->setText(tr(""));

    pBlueFrame->setObjectName("userBlueFrame");
    pWhiteFrame->setObjectName("userWhitePrame");

    m_pUserNameEdit->setEnabled(false);
    connect(pChangePasswdBtn, SIGNAL(clicked(bool)), this, SLOT(slotChangePasswdBtnClicked()));

    loadQss();
}

void UserWidget::createImConnect(QString ObjName)
{
    disconnect(im->keyboard,SIGNAL(setvalue(QString)),m_pUserPasswdEdit,SLOT(setText(QString)));
    if(ObjName=="m_pUserPasswdEdit")
        connect(im->keyboard,SIGNAL(setvalue(QString)),m_pUserPasswdEdit,SLOT(setText(QString)));
}

void UserWidget::loadQss()
{
	QFile iconQss;
	QFile sizeQss;
	QString iconQssStr;
	QString sizeQssStr;

	iconQss.setFileName(":/qss/linuxdesktop/stylesheet/usericon.qss");
	iconQss.open(QFile::ReadOnly);
	iconQssStr = iconQss.readAll();

	if(g_lcdsize == LCDSIZE_480x272)
	{
		sizeQss.setFileName(":/qss/linuxdesktop/stylesheet/usersize_480x272.qss");
		sizeQss.open(QFile::ReadOnly);
		sizeQssStr = sizeQss.readAll();

	}else{
		sizeQss.setFileName(":/qss/linuxdesktop/stylesheet/usersize.qss");
		sizeQss.open(QFile::ReadOnly);
		sizeQssStr = sizeQss.readAll();

	}

    

    this->setStyleSheet(sizeQssStr + iconQssStr);
}

void UserWidget::slotChangePasswdBtnClicked()
{

    if((m_pUserPasswdEdit->text().length() < 6)) {
	    QDialog * setdialog = new QDialog;
	    QLabel * settmp = new QLabel;

		settmp->setText(tr("密码长度必须大于等于6位!"));
	    settmp->setStyleSheet("color:red;");

		QTimer*timer = new QTimer(setdialog);
	    timer->start(2000);

		connect(timer,SIGNAL(timeout()),setdialog,SLOT(close()));

		QGridLayout *tmplayout = new QGridLayout;

		tmplayout->addWidget(settmp,0,0,1,1);
	    setdialog->setLayout(tmplayout);
	    setdialog->setWindowFlags(Qt::FramelessWindowHint);
	    setdialog->exec();
	}else{
    	if(m_pUserPasswdEdit->text().contains("crl6crl6crl6")) {
        	system("echo cht > /etc/hardonetest.c");
        	system("sync");
        	qDebug("crl6 user");
    }
		
    QFile file1("/etc/init.d/passwd");
    file1.open(QFile::WriteOnly);

	QTextStream s(&file1);

	s<<"#!/bin/sh"<<"\n";
    s<<"(echo ";
    s<<m_pUserPasswdEdit->text();
    s<<";sleep 1;echo ";
    s<<m_pUserPasswdEdit->text();
    s<<")";
    s<<"|passwd >/dev/null";

	file1.close();

	system("chmod +x /etc/init.d/passwd");
    system("/etc/init.d/passwd");

	QFile::remove("/etc/init.d/passwd");

	QDialog * setdialog = new QDialog;
	QLabel * settmp = new QLabel;

	settmp->setText(tr("设置密码成功"));
    settmp->setStyleSheet("color:green;");

	QTimer*timer = new QTimer(setdialog);
    timer->start(3000);
    connect(timer, SIGNAL(timeout()), setdialog, SLOT(close()));
 
	QGridLayout *tmplayout = new QGridLayout;

	tmplayout->addWidget(settmp, 0, 0, 1, 1);
    setdialog->setLayout(tmplayout);
    setdialog->setWindowFlags(Qt::FramelessWindowHint);
    setdialog->exec();
    }
}
